package com.sgb.auction.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.auction.R;
import com.sgb.auction.databinding.FocusAuctionFragmentBinding;
import com.sgb.auction.entity.AuctionListEntity;
import com.sgb.auction.adapter.FocusAuctionAdapter;
import com.sgb.auction.entity.RequestBean;
import com.sgb.auction.viewmodule.FocusAuctionListModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * Time:2021/12/9
 * AUTHOR:F_Q
 * Description:拍卖只能关注
 */
public class FocusAuctionListFragment extends AppLazyFragment {

    private FocusAuctionListModel mModel;
    private FocusAuctionAdapter focusauctionadapter;
    private boolean isLoadMore = false;
    private RequestBean requestBean = new RequestBean();
    private FocusAuctionFragmentBinding mBinding;
    public boolean isInIt = false;

    public static FocusAuctionListFragment newInstance() {
        FocusAuctionListFragment fragment = new FocusAuctionListFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.focus_auction_fragment, container, false);
        mModel = new ViewModelProvider(this).get(FocusAuctionListModel.class);
        mModel.setActivity(FocusAuctionListFragment.this);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        super.initView();
        if (focusauctionadapter == null) {
            focusauctionadapter = new FocusAuctionAdapter(getActivity(), null);
        }
        mBinding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mBinding.recyclerView.setAdapter(focusauctionadapter);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initDataList();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                requestBean.page++;
                //上拉加载更多
                // mModel.AuctionHallList(requestBean);
            }
        });
        focusauctionadapter.setOnItemClick(new AdapterOnItemClick<AuctionListEntity>() {
            @Override
            public void onItemClick(AuctionListEntity auctionListEntity, int position) {
                if (auctionListEntity.getState() == 4 || auctionListEntity.getState() == 5) {

                } else {
                    mModel.getWebView(auctionListEntity.getNo());
                }
            }
        });
        isInIt = true;
    }

    private void initDataList() {
        isLoadMore = false;
        requestBean.page = 1;
        requestBean.setSortMethod("1");
        requestBean.setUserFocus("1");
        mModel.getFocusAuctionList(requestBean);
    }

    @Override
    protected void onLazyLoad() {
        initDataList();
    }

    public void setFocusAuctionList(List<AuctionListEntity> data, boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                focusauctionadapter.addDatas(data);
            }
            mBinding.recyclerView.setNoMore(isLastPage);
        } else {
            if (data != null && data.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                focusauctionadapter.setDatas(data);
                mBinding.recyclerView.scrollToPosition(0);
                mBinding.recyclerView.setNoMore(isLastPage);
            } else {
                focusauctionadapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.GONE);
            }
        }
    }


    private void RecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100581) {
            onLazyLoad();
        }
    }
}
